import { GET_COMMENTS, ADD_COMMENTS, DEL_COMMENTS } from './action-types';

const initComments = []
export const comments = (state = initComments, action) => {
    switch (action.type) {
        case GET_COMMENTS:
            return action.data;
            break;
        case ADD_COMMENTS:
            const arr = [...state];
            const id=state.length>0?state[0].id+1:1;
            arr.unshift({ id, ...action.data });
            return arr;
            break;
        case DEL_COMMENTS:
            return state.filter((item) => item.id !== action.data);
            break;
        default:
            return state;
    }
}